//
//  PickNumSFCViewController.m
//  PlayBall
//
//  Created by Sun Fanglei on 13-5-28.
//
//

#import "JddPickNumSFCViewController.h"
#import "SBJson.h"
#import "JddViewControllerManager.h"

@interface JddPickNumSFCViewController ()

@end

@implementation JddPickNumSFCViewController

@synthesize listTable;
@synthesize count;
@synthesize currentIssue;
@synthesize sportLotterylist;
@synthesize sResult;
@synthesize Matchs;
@synthesize Matchtemp;
@synthesize endtimeLavel;
@synthesize endTime;
@synthesize issueID;
@synthesize issueName;
@synthesize haveSelectLabel;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddPickNumSFCViewController);

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75.0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [Matchs count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JddPickNumSportItemCell *cell = [JddPickNumSportItemCell getInstanceView];
    [cell.label_isSingle setHidden:YES];
    NSDictionary *matchCell = [Matchs objectAtIndex:indexPath.row];
    NSString *ETime = [matchCell objectForKey:@"MatchTime"];
    NSString *HTeam = [matchCell objectForKey:@"HTeam"];
    NSString *MID = [matchCell objectForKey:@"No"];
    NSString *SPSPF = [matchCell objectForKey:@"OuOdds"];
    NSArray *Pl =[SPSPF componentsSeparatedByString:@" "];
    NSString *VTeam = [matchCell objectForKey:@"VTeam"];
    cell.weakNum.text = [NSString stringWithFormat:@"%@",MID];
    cell.EndTime.text = @"比赛时间";
    cell.endtime = ETime;
    cell.Match.text = [NSString stringWithFormat:@"%@",[ETime substringWithRange:NSMakeRange(5,11)]];
    cell.HTeam.text= HTeam;
    cell.VTeam.text = VTeam;
    cell.MID = MID;
    if ([Pl count] >= 3) {
//        cell.slabel.text = [NSString stringWithFormat:@"胜%1.2f", [[Pl objectAtIndex:0] floatValue]];
//        cell.plabel.text = [NSString stringWithFormat:@"平%1.2f", [[Pl objectAtIndex:1] floatValue]];
//        cell.flabel.text = [NSString stringWithFormat:@"负%1.2f", [[Pl objectAtIndex:2] floatValue]];
        
        NSString *winStr = [NSString stringWithFormat:@"胜%1.2f", [[Pl objectAtIndex:0] floatValue]];
        [cell.Win setTitle:winStr forState:UIControlStateNormal];
        NSString *drweStr = [NSString stringWithFormat:@"平%1.2f", [[Pl objectAtIndex:1] floatValue]];
        [cell.Drew setTitle:drweStr forState:UIControlStateNormal];
        NSString *loseStr = [NSString stringWithFormat:@"负%1.2f", [[Pl objectAtIndex:2] floatValue]];
        [cell.Lose setTitle:loseStr forState:UIControlStateNormal];
    }
    
//    cell.slabel.textColor = [UIColor blackColor];
//    cell.plabel.textColor = [UIColor blackColor];
//    cell.flabel.textColor = [UIColor blackColor];
    cell.aSportNumber = [[JddSportNumber alloc] init];
    cell.bSportNumber = [[JddSportNumber alloc] init];
    cell.cSportNumber = [[JddSportNumber alloc] init];
    cell.cSportNumber.code = MID;
    cell.cSportNumber.Hteam = HTeam;
    cell.cSportNumber.Vteam = VTeam;
    cell.cSportNumber.endTime = ETime;
    cell.cSportNumber.teamArray= [[NSMutableArray alloc] initWithObjects:@"0", nil];
    cell.bSportNumber.code = MID;
    cell.bSportNumber.Hteam = HTeam;
    cell.bSportNumber.Vteam = VTeam;
    cell.bSportNumber.endTime = ETime;
    cell.bSportNumber.teamArray= [[NSMutableArray alloc] initWithObjects:@"1", nil];
    cell.aSportNumber.code = MID;
    cell.aSportNumber.Hteam = HTeam;
    cell.aSportNumber.Vteam = VTeam;
    cell.aSportNumber.endTime = ETime;
    cell.aSportNumber.teamArray= [[NSMutableArray alloc] initWithObjects:@"3", nil];
    cell.delegate = self;
    for(JddSportNumber *num in sportLotterylist.sportList){
        if ([num.code isEqualToString:MID]) {
            for (NSNumber *num2 in num.teamArray) {
                switch ([num2 intValue]) {
                    case 3:
                        [cell.Win setSelected:YES];
                        
                        break;
                    case 1:
                        [cell.Drew setSelected:YES];
                        
                        break;
                    case 0:
                        [cell.Lose setSelected:YES];
                        
                        break;
                        
                    default:
                        break;
                }
            }
        }
        
    }
    return cell;
}

-(void)sportTeamButtonClicked:(id)sender number:(JddSportNumber *)number1{
    UIButton *button = sender;
    JddSportNumber *number = [[JddSportNumber alloc] init];
    number.code = number1.code;
    number.teamArray = [NSMutableArray arrayWithArray:number1.teamArray];
    number.endTime = number1.endTime;
    number.Hteam= number1.Hteam;
    number.Vteam = number1.Vteam;
    [self.view setUserInteractionEnabled:NO];
    if (button.tag == 144) {
        if (![sportLotterylist BallHasNumber:number]) {
            [button setSelected:NO];
        }else {
            
            if ([sportLotterylist.sportList count]>30) {
                [sportLotterylist BallHasNumber:number];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"最多选择30场比赛" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [button setSelected:NO];
            }else {
                [button setSelected:YES];
            }
            
        }
    }
    sportLotterylist.sportList = [NSMutableArray arrayWithArray:[sportLotterylist.sportList sortedArrayUsingComparator:^(id obj1, id obj2) {
        JddSportNumber *sport1 = (JddSportNumber*)obj1;
        JddSportNumber *sport2 = (JddSportNumber*)obj2;
        if ([sport1.code intValue]>[sport2.code intValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        } else {
            return (NSComparisonResult)NSOrderedAscending;
        }
    }]];
    
    self.haveSelectLabel.text = [NSString stringWithFormat:@"已选择%d场比赛",[sportLotterylist.sportList count]];
    [self.view setUserInteractionEnabled:YES];
}

-(void)listSelect{
//    CGRect rect = [[UIScreen mainScreen] bounds];
//    rect.origin.x = (rect.size.width) / 2.0;
//    rect.origin.y = (rect.size.height) / 2.0;
//    rect.size.width = 251.0;
//    rect.size.height = 300.0;
//    indexList = [JddIndexListView getInstanceView];
//    [indexList setFrame:rect];
//    indexList.type = 1;
//    indexList.delegate = self;
//    [indexList show];
    
    NoticeViewController *noticeView = [[NoticeViewController alloc] init];
    noticeView.webInfoId = LOTTERY_TYPE_ID_SF;
    [self.navigationController pushViewController:noticeView animated:YES];

}

//-(void)selectorbutton1{
////    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BD_RESULT
////                                                         fromController:self
////                                                             withObject:nil
////                                                            withObject2:nil];
//    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_OPEN_LIST
//                                                         fromController:self
//                                                             withObject:@"1"
//                                                            withObject2:nil];
//}
//
//-(void)selectorbutton2{
//    if (![JddCommonFunction isLogin]) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
//                                                        message:@"您还没有登录，请先登录"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:@"取消", nil]
//        ;
//        [alert show];
//    }else {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BUY_HISTORY
//                                                             fromController:self
//                                                                 withObject:[NSNumber numberWithInt:LOTTERY_TYPE_ID_SF]
//                                                                withObject2:nil];
//    }
//}
//
//-(void)selectorbutton3{
//    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_HOW_TO_PLAY_SSQ
//                                                         fromController:self
//                                                             withObject:nil
//                                                            withObject2:nil];
//}
//
//-(void)selectorbutton4{
//    if (![JddCommonFunction isLogin]) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
//                                                        message:@"您还没有登录，请先登录"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:@"取消", nil]
//        ;
//        [alert show];
//    }else {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_FOLLOW_NUMBER_HISTORY
//                                                             fromController:self
//                                                                 withObject:nil
//                                                                withObject2:nil];
//    }
//}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.message isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:
             CONTROLLER_LOGIN
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }
    }
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)loadNavBar
{
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 40, 30);
    [right setTitle:@"帮助" forState:UIControlStateNormal];
    right.titleLabel.font = [UIFont systemFontOfSize:15];
    [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(listSelect) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    
}
//- (void)backView
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self.navigationItem setTitle:@"胜负彩"];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:@"胜负彩"];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;

    [self loadNavBar];
    sportLotterylist = [[JddSportNumberList alloc] init];
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    listTable.tableFooterView = v;

    [listTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [listTable setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setCurrentIssue:nil];
    [self setEndtimeLavel:nil];
    [self setHaveSelectLabel:nil];
    [self setListTable:nil];
    [super viewDidUnload];
}

-(void)QuerySFCIssueOperationResult:(NSData *)result{
    if (result) {
        sResult  =  [JddDataParser parseSportLotteryIssueResult:result];
        SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
        issueID = [[jsonParser objectWithString:sResult] objectForKey:@"issueID"];
        issueName = [[jsonParser objectWithString:sResult] objectForKey:@"issue"];
        Matchs = [[jsonParser objectWithString:sResult] objectForKey:@"Matches"];
        endTime = [[jsonParser objectWithString:sResult] objectForKey:@"EndTime"];
        self.haveSelectLabel.text = [NSString stringWithFormat:@"已选择%d场比赛",[sportLotterylist.sportList count]];
        outValue = 0;
        self.currentIssue.text = issueName;
        self.endtimeLavel.text = endTime;
        
        
        [listTable reloadData];
    }
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"SFC"];
	[MobClick event:@"Lottery-SFC"];
    
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQuerySFCIssueOperation *operationsfc = [[JddQuerySFCIssueOperation alloc] init];
    operationsfc.delegate = self;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operationsfc];    
}

-(void)destoryView{
    if ([sportLotterylist.sportList count]>0) {
        [sportLotterylist.sportList removeAllObjects];
    }
    [listTable reloadData];
    self.haveSelectLabel.text = @"已选择0场比赛";
}

-(void)viewWillDisappear:(BOOL)animated{
    outValue = 1;
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"SFC"];

}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)clearBtnClick:(id)sender {
    [sportLotterylist.sportList removeAllObjects];
    [listTable reloadData];
    self.haveSelectLabel.text = @"已选择0场比赛";
}

- (IBAction)doneBtnClick:(id)sender {
    if ([sportLotterylist.sportList count]<14&&outValue==0) {
        [JddCommonFunction messageBoxWithMessage:@"胜负彩必须选择14场比赛"];
        return;
    } else{
        if ([sportLotterylist.sportList count]>0) {                      
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_SFC_LIST
                                                                 fromController:self
                                                                     withObject:sportLotterylist.sportList
                                                                    withObject2:endTime];
        }else{
            return;
        }
        
    }
}
@end
